package com.cci.kangdao.service.impl;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cci.kangdao.aop.UserThreadLocal;
import com.cci.kangdao.dao.LoginDao;
import com.cci.kangdao.dto.response.AuthInfoResponse;
import com.cci.kangdao.dto.response.EWUrlResponse;
import com.cci.kangdao.model.AuthInfoModel;
import com.cci.kangdao.model.condition.AuthInfoCondition;
import com.cci.kangdao.service.EmissionWarrantyService;
import com.cci.kangdao.utilTool.BeanUtils;
import com.cci.kangdao.utilTool.NumberUtils;
import com.cci.kangdao.utilTool.ParameterTool;
import com.cci.kangdao.utilTool.PropertiesUtil;
import com.cci.kangdao.utilTool.RequestContextHolder;
import com.cci.kangdao.utilTool.TokenUtils;

@Service
public class EmissionWarrantyServiceImpl implements EmissionWarrantyService {

	private final Logger logger = Logger.getLogger(getClass());

	@Autowired
	private LoginDao loginDao;

	@Override
	public void getUserIdAndToken() {
		String emissionWarrantyUrl = PropertiesUtil.getProperties("projectconfig.properties", "EmissionWarrantyUrl");
		String adUserId = UserThreadLocal.get().getAduserId();
		String token = TokenUtils.getToken();
		String url = String.format(emissionWarrantyUrl, adUserId, token);
		ParameterTool.writeSuccessResponse(RequestContextHolder.getResponse(), new EWUrlResponse(url));
	}

	@Override
	public void getAuthInfo(AuthInfoCondition condition) {
		String userID = condition.getUserID();
		String token = condition.getToken();
		// 如果参数为空响应错误
		if (StringUtils.isNotBlank(userID) && StringUtils.isNotBlank(token)) {
			// 校验当前token是否有效
			if (TokenUtils.validateToken(token)) {
				AuthInfoModel authInfoModel = loginDao.getAuthInfoByAdUserId(userID);
				if (null != authInfoModel) {
					// 拷贝对象属性
					AuthInfoResponse response = BeanUtils.convert(authInfoModel, AuthInfoResponse.class);

					// userName的第一个字符
					String userName = authInfoModel.getUserName();
					if (StringUtils.isNotBlank(userName)) {
						response.setUserName(userName.substring(0, 1));
					}

					// 手机号的后4位
					String phone = authInfoModel.getPhone();
					if (StringUtils.isNotBlank(phone)) {
						if (phone.length() > 4) {
							response.setPhone(phone.substring(phone.length() - 4));
						} else {
							response.setPhone(phone);
						}
					}

					// organizationName、organizationID
					if (NumberUtils.isPositiveNum(authInfoModel.getCompanyId())) {
						response.setOrganizationID(authInfoModel.getCompanyNo());
						response.setOrganizationName(authInfoModel.getCompanyName());
					}else {
						response.setOrganizationID(authInfoModel.getStationNo());
						response.setOrganizationName(authInfoModel.getStationName());
					}

					ParameterTool.writeSuccessResponse(RequestContextHolder.getResponse(), response);
				} else {
					logger.error(String.format("请求参数错误：userID:%s token:%s", userID, token));
					ParameterTool.writeResponse(RequestContextHolder.getResponse(), 3, "查无用户信息");
				}

			} else {
				logger.error(String.format("请求参数错误：userID:%s token:%s", userID, token));
				ParameterTool.writeResponse(RequestContextHolder.getResponse(), 2, "EW验证Token错误");
			}
		} else {
			logger.error(String.format("请求参数错误：userID:%s token:%s", userID, token));
			ParameterTool.writeResponse(RequestContextHolder.getResponse(), 1, "EW验证必填参数错误");
		}
	}


}
